package com.estate.struts.actions;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.estate.business.TenderCopyService;
import com.estate.utils.ProjectConstants;
import com.estate.vo.Vo;
import com.estate.vo.impl.TenderCopyVo;
import com.estate.vo.impl.TenderVo;

public class TenderCopyAction extends EstateSupport {

	private String id;
	private String tenderId;
	private String fileNum;
	private String fileOrig;
	private String fileCopy;
	private String filePostDate;
	private String answerDate;
	private String answerPlace;
	private String quaranteeMoney;
	private String highMoney;
	private String evaluType;
	private String sameTender;
	private String tenderFileWriter;
	private String writerRole;
	private String writerQuali;
	private String handlePeople;
	private String handleCompany;
	private String handlePhone;
	private String copyDate;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private TenderCopyService tenderCopyService;
	
	private String directPage;
	
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if("add".equals(this.getMethod())){
			TenderCopyVo tenderCopy = this.getAddTenderCopy();
			if(tenderCopy != null){
				boolean result = this.getTenderCopyService().addTenderCopy(tenderCopy);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<Vo> list = this.getTenderCopyService().getList();
			this.getRequest().setAttribute("list", list);
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allTenderCopy";
		}else if("detail".equals(this.getMethod())){
			Vo vo = this.getTenderCopyService().getTenderCopy(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderCopy", vo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo vo = this.getTenderCopyService().getTenderCopy(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderCopy", vo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			TenderCopyVo tenderCopy = this.getUpdateTenderCopy();
			if(tenderCopy == null){
				page = "fail";
			}else{
				boolean result = this.getTenderCopyService().updateTenderCopy(tenderCopy);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}
//		else if("post".equals(this.getMethod())){
//			this.getAnnouncementService().updatePost(Integer.parseInt(this.getId()));
//			page = this.SUCCESS;
//		}
		else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
//				System.out.println(this.getDelStr());
//				System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getTenderCopyService().deleteMultiTenderCopy(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
//				System.out.println("single delete");
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					TenderCopyVo tenderCopy = new TenderCopyVo();
					tenderCopy.setId(Integer.parseInt(this.getId()));
					boolean result = this.getTenderCopyService().deleteTenderCopy(tenderCopy);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else{
			page="fail";
		}
		if(this.getDirectPage() != null && !"".equals(this.getDirectPage())){
			page = this.getDirectPage();
		}
		return page;
	}
	private TenderCopyVo getUpdateTenderCopy(){
		TenderCopyVo tenderCopy = new TenderCopyVo();
		
		int id = Integer.parseInt(this.getId());
		
		try {
			tenderCopy.setId(id);
			tenderCopy.setAnswerDate(ProjectConstants.SIMPLE_DATE.parse(this.getAnswerDate()));
			tenderCopy.setAnswerPlace(this.getAnswerPlace());
			tenderCopy.setCopyDate(ProjectConstants.SIMPLE_DATE.parse(this.getCopyDate()));
			tenderCopy.setEvaluType(this.getEvaluType());
			tenderCopy.setFileCopy(Integer.parseInt(this.getFileCopy()));
			tenderCopy.setFileNum(Integer.parseInt(this.getFileNum()));
			tenderCopy.setFileOrig(Integer.parseInt(this.getFileOrig()));
			tenderCopy.setFilePostDate(ProjectConstants.SIMPLE_DATE.parse(this.getFilePostDate()));
			tenderCopy.setHandleCompany(this.getHandleCompany());
			tenderCopy.setHandlePeople(this.getHandlePeople());
			tenderCopy.setHandlePhone(this.getHandlePhone());
			tenderCopy.setHighMoney(this.getHighMoney());
			tenderCopy.setQuaranteeMoney(this.getQuaranteeMoney());
			tenderCopy.setSameTender(Integer.parseInt(this.getSameTender()));
			tenderCopy.setTenderFileWriter(this.getTenderFileWriter());
			tenderCopy.setWriterQuali(this.getWriterQuali());
			tenderCopy.setWriterRole(this.getWriterRole());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		
		return tenderCopy;
	}
	private TenderCopyVo getAddTenderCopy(){
		TenderCopyVo tenderCopy = new TenderCopyVo();
		
		try {
			tenderCopy.setAnswerDate(ProjectConstants.SIMPLE_DATE.parse(this.getAnswerDate()));
			tenderCopy.setAnswerPlace(this.getAnswerPlace());
			tenderCopy.setCopyDate(ProjectConstants.SIMPLE_DATE.parse(this.getCopyDate()));
			tenderCopy.setEvaluType(this.getEvaluType());
			tenderCopy.setFileCopy(Integer.parseInt(this.getFileCopy()));
			tenderCopy.setFileNum(Integer.parseInt(this.getFileNum()));
			tenderCopy.setFileOrig(Integer.parseInt(this.getFileOrig()));
			tenderCopy.setFilePostDate(ProjectConstants.SIMPLE_DATE.parse(this.getFilePostDate()));
			tenderCopy.setHandleCompany(this.getHandleCompany());
			tenderCopy.setHandlePeople(this.getHandlePeople());
			tenderCopy.setHandlePhone(this.getHandlePhone());
			tenderCopy.setHighMoney(this.getHighMoney());
			tenderCopy.setPostDate(new Date());
			tenderCopy.setPostUser(this.getSessionUser());
			tenderCopy.setQuaranteeMoney(this.getQuaranteeMoney());
			tenderCopy.setSameTender(Integer.parseInt(this.getSameTender()));
			tenderCopy.setTenderFileWriter(this.getTenderFileWriter());
			tenderCopy.setWriterQuali(this.getWriterQuali());
			tenderCopy.setWriterRole(this.getWriterRole());
			
			TenderVo tender = new TenderVo();
			tender.setId(Integer.parseInt(this.getTenderId()));
			
			tenderCopy.setTender(tender);
		} catch (ParseException e) {
			e.printStackTrace();
		}
			
		return tenderCopy;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTenderId() {
		return tenderId;
	}
	public void setTenderId(String tenderId) {
		this.tenderId = tenderId;
	}
	public String getFileNum() {
		return fileNum;
	}
	public void setFileNum(String fileNum) {
		this.fileNum = fileNum;
	}
	public String getFileOrig() {
		return fileOrig;
	}
	public void setFileOrig(String fileOrig) {
		this.fileOrig = fileOrig;
	}
	public String getFileCopy() {
		return fileCopy;
	}
	public void setFileCopy(String fileCopy) {
		this.fileCopy = fileCopy;
	}
	public String getFilePostDate() {
		return filePostDate;
	}
	public void setFilePostDate(String filePostDate) {
		this.filePostDate = filePostDate;
	}
	public String getAnswerDate() {
		return answerDate;
	}
	public void setAnswerDate(String answerDate) {
		this.answerDate = answerDate;
	}
	public String getAnswerPlace() {
		return answerPlace;
	}
	public void setAnswerPlace(String answerPlace) {
		this.answerPlace = answerPlace;
	}
	public String getQuaranteeMoney() {
		return quaranteeMoney;
	}
	public void setQuaranteeMoney(String quaranteeMoney) {
		this.quaranteeMoney = quaranteeMoney;
	}
	public String getHighMoney() {
		return highMoney;
	}
	public void setHighMoney(String highMoney) {
		this.highMoney = highMoney;
	}
	public String getEvaluType() {
		return evaluType;
	}
	public void setEvaluType(String evaluType) {
		this.evaluType = evaluType;
	}
	public String getSameTender() {
		return sameTender;
	}
	public void setSameTender(String sameTender) {
		this.sameTender = sameTender;
	}
	public String getTenderFileWriter() {
		return tenderFileWriter;
	}
	public void setTenderFileWriter(String tenderFileWriter) {
		this.tenderFileWriter = tenderFileWriter;
	}
	public String getWriterRole() {
		return writerRole;
	}
	public void setWriterRole(String writerRole) {
		this.writerRole = writerRole;
	}
	public String getWriterQuali() {
		return writerQuali;
	}
	public void setWriterQuali(String writerQuali) {
		this.writerQuali = writerQuali;
	}
	public String getHandlePeople() {
		return handlePeople;
	}
	public void setHandlePeople(String handlePeople) {
		this.handlePeople = handlePeople;
	}
	public String getHandleCompany() {
		return handleCompany;
	}
	public void setHandleCompany(String handleCompany) {
		this.handleCompany = handleCompany;
	}
	public String getHandlePhone() {
		return handlePhone;
	}
	public void setHandlePhone(String handlePhone) {
		this.handlePhone = handlePhone;
	}
	public String getCopyDate() {
		return copyDate;
	}
	public void setCopyDate(String copyDate) {
		this.copyDate = copyDate;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public TenderCopyService getTenderCopyService() {
		return tenderCopyService;
	}
	public void setTenderCopyService(TenderCopyService tenderCopyService) {
		this.tenderCopyService = tenderCopyService;
	}
	public String getDirectPage() {
		return directPage;
	}
	public void setDirectPage(String directPage) {
		this.directPage = directPage;
	}
}
